package com.jrx.anytxn.reporting.job.lsSecondFile.step;

import com.jrx.anytxn.reporting.config.LSFileConfig;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.service.ILSFileService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 生成第二代征信个人关联信息 Writer
 *
 * @author liuyang
 * @date 2021-04-26
 */
@Service
public class LSSecondCustContactFileWriter implements ItemWriter<List> {

    @Autowired
    private ILSFileService fileService;
    @Autowired
    private LSFileConfig fileConfig;

    @Override
    public void write(List<? extends List> items) throws Exception {
        if (CollectionUtils.isNotEmpty(items)) {
            List<String> messageList = new ArrayList<>();
            for (List item : items) {
                if (CollectionUtils.isNotEmpty(item)) {
                    messageList.addAll(item);
                }
            }
            if (CollectionUtils.isNotEmpty(messageList)) {
                fileService.generateLSFile(messageList, fileConfig.getLsSecondFilePath(), fileConfig.getLsFileName(),
                        ReportingConstant.LS_SECOND_FILE_RELTINFO, ReportingConstant.LS_SECOND_FILE_SUFFIX, ReportingConstant.LS_SECOND_FILE_CHARSET);
            }
        }
    }
}
